package com.fulin.spring;

import com.fulin.spring.beans.Context;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class MySpring {

    private static ThreadPoolTaskExecutor executor;

    private static ThreadPoolTaskExecutor subExecutor;

    private static MySpring mySpring = new MySpring();

    private static DefaultListableBeanFactory factory = null;

    private static Context context = null;

    public static void main(String[] args) throws InterruptedException {
        //创建资源
        Resource resource = new ClassPathResource("spring.xml");

        //创建工厂
        factory = new DefaultListableBeanFactory();

        //创建资源读取器
        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(factory);

        //读取资源
        beanDefinitionReader.loadBeanDefinitions(resource);

        executor = factory.getBean(ThreadPoolTaskExecutor.class);

        subExecutor = (ThreadPoolTaskExecutor)factory.getBean("subThreadPoolTaskExecutor");

        for(int i = 1; i<= 283; i++){
            executor.execute(mySpring::doBiz);
        }

        while(true){
            Thread.sleep(2000L);

            System.out.println("当前活跃线程数：" + executor.getThreadPoolExecutor().getActiveCount());
            System.out.println("当前活跃线程数：" + subExecutor.getThreadPoolExecutor().getActiveCount());
            System.out.println("当前线程池线程总数：" + executor.getThreadPoolExecutor().getPoolSize());
            System.out.println("当前线程池线程总数：" + subExecutor.getThreadPoolExecutor().getPoolSize());
            System.out.println("到目前为止，一共创建的线程数：" + executor.getThreadPoolExecutor().getLargestPoolSize());
            System.out.println("到目前为止，一共创建的线程数：" + subExecutor.getThreadPoolExecutor().getLargestPoolSize());
            System.out.println("到目前为止，一共处理的任务数：" + executor.getThreadPoolExecutor().getTaskCount());
            System.out.println("到目前为止，一共处理的任务数：" + subExecutor.getThreadPoolExecutor().getTaskCount());
            System.out.println("到目前为止，一共完成的任务数：" + executor.getThreadPoolExecutor().getCompletedTaskCount());
            System.out.println("到目前为止，一共完成的任务数：" + subExecutor.getThreadPoolExecutor().getCompletedTaskCount());
            System.out.println();
        }
    }

    private void doBiz(){
        //模拟数据查询操作
        int i = 0;
        while(i < 1000000){
            Math.sqrt(i);
            i++;
        }

        //模拟第一次文件生成
        Context context = factory.getBean(Context.class);
        context.inc();
        subExecutor.execute(context::dec);
        combin(context);

        //模拟第二次文件生成
        Context context2 = factory.getBean(Context.class);
        context2.inc();
        subExecutor.execute(context2::dec);
        combin(context2);

        //模拟第三次文件生成
        Context context3 = factory.getBean(Context.class);
        context3.inc();
        subExecutor.execute(context3::dec);
        combin(context3);

    }

    private void combin(Context context){
        while(true){
            if(context.get() != 0){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                return;
            }
        }
    }

}
